package week07

import (
	"bytes"
	"math"
)

// 14. 最长公共前缀
// https://leetcode-cn.com/problems/longest-common-prefix/

func longestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}

	var buf = &bytes.Buffer{}
	var m = math.MaxInt32
	for _, str := range strs {
		m = min(m, len(str))
	}
	for i := 0; i < m; i++ {
		var b = strs[0][i]
		for _, str := range strs[1:] {
			if b != str[i] {
				return buf.String()
			}
		}
		buf.WriteByte(b)
	}
	return buf.String()
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}